package com.csist.server.task;

import com.csist.dao.MessageDao;
import com.csist.model.Message;
import com.csist.model.User;
import com.csist.server.Server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Iterator;

public class ServerRunnable implements Runnable{
    private Socket socket;
    private ObjectInputStream ois = null;
    private ObjectOutputStream oos = null;
    private boolean state = true;
    private MessageDao mDao = new MessageDao();
    @Override
    public void run() {
        while (state){
            Object obj = null;
            try {
                ois = new ObjectInputStream(socket.getInputStream());
                obj = ois.readObject();

                //读取user第一次发送过来的user对象
                if (obj instanceof User){
                    User user = (User)obj;
                    user.setSocket(socket);
                    Server.users.add(user);
                    System.out.println(user.getName() + user.getId());

                    /*
                    发送离线消息
                    mDao.getMessage(user.getId())--获取接受者为user.getId()，并且状态为false(未被接收)的消息实体集合
                    客户端一连接就会查询数据库的消息信息
                     */
                    ArrayList<Message> messages = mDao.getMessage(user.getId());
                    for (Message m :messages) {
                        oos = new ObjectOutputStream(socket.getOutputStream());
                        oos.writeObject(m);
                        oos.flush();
                    }
                    //设置信息的状态为true(已接收)
                    mDao.setStateTrue(user.getId());
                }

                //读取用户发送过来的信息实体对象
                if (obj instanceof Message){
                    Message message = (Message) obj;

                    //如果接收的消息对象的接收者为0,代表这是一条群消息，则发送给所有在线用户
                    if (message.getReceiver()==0){
                        for (User user:Server.users) {
                            oos = new ObjectOutputStream(user.getSocket().getOutputStream());
                            oos.writeObject(message);
                            oos.flush();
                        }
                    } else { //否则这条消息是一条私法消息，发送给特定用户
                        for (User user:Server.users) {
                            if(user.getId()==message.getReceiver()){//如果这个用户在线的话，发送给他
                                oos = new ObjectOutputStream(user.getSocket().getOutputStream());
                                oos.writeObject(message);
                                oos.flush();
                                mDao.setStateTrue(user.getId());//数据库设置这条消息为以接收状态
                                break;
                            }
                        }
                    }

                }
            } catch (IOException e) {
                close();
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                close();
                e.printStackTrace();
            }
        }
    }

    public ServerRunnable(Socket socket) {
        this.socket = socket;
    }
    public void close(){
        this.state = false;
        Iterator iterator = Server.users.iterator();
        synchronized (Server.users){
            while (iterator.hasNext()){
                iterator.next();
                iterator.remove();
            }
        }

    }
}
